package cn.phonevane.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import cn.phonevane.dao.BaseDao;
import cn.phonevane.model.TArticle;
import cn.phonevane.service.ArticleService;
/**
 * ��������ʵ��
 * 
 * @author xuanaiwu
 */
public class ArticleServiceImpl implements ArticleService{
	
	private BaseDao baseDao;
	
	public BaseDao getBaseDao() {
		return baseDao;
	}

	public void setBaseDao(BaseDao baseDao) {
		this.baseDao = baseDao;
	}

	/**
	 * 保存一篇文章
	 * @param object 
	 * @return boolean
	 */
	public boolean add(Object object) {
		
		return baseDao.save(object);
	}
	
	/**
	 * 获取所有文章
	 * @return 集合
	 */
	public List<TArticle> get(){
		List<TArticle> tArticleList=new ArrayList<TArticle>();
		int first=0,count=5;
		String hql="select a.articleId,a.userName,a.articleTitle,a.articleContent,a.articleCreateTime" +
				" from TArticle as a order by a.articleCreateTime desc";
		List list=this.baseDao.find(hql, first, count);
		Iterator it=list.iterator();
		while(it.hasNext()){
			TArticle tArticle=new TArticle();
			Object[] results=(Object[])it.next();
			tArticle.setArticleId((Long)results[0]);
			tArticle.setUserName((String)results[1]);
			tArticle.setArticleTitle((String)results[2]);
			tArticle.setArticleContent((String)results[3]);
			tArticle.setArticleCreateTime((Date)results[4]);
			
			tArticleList.add(tArticle);//装载tArticle对象
			
		}
		return tArticleList;
		
	}
	
	
	/**
	 * 获取前20篇文章
	 * @return 集合
	 */
	public List<TArticle> getPart(int num) {
		
		int first=0;
		List<TArticle> tArticleList=new ArrayList<TArticle>();
		String hql="select a.articleId,a.articleTitle,articleKeywords,a.articleCreateTime from TArticle as a where a.articleIfAuthority='"+1+"' order by a.articleCreateTime desc";
		List list=this.baseDao.find(hql, first, num);
		Iterator it=list.iterator();
		while(it.hasNext()){
			TArticle tArticle=new TArticle();
			Object[] results=(Object[])it.next();
			tArticle.setArticleId((Long)results[0]);
			tArticle.setArticleTitle((String)results[1]);
			tArticle.setArticleKeywords((String)results[2]);
			tArticle.setArticleCreateTime((Date)results[3]);
			tArticleList.add(tArticle);
		}
		return tArticleList;

	}
	
	/**
	 * 根据文章ID查找获取文章
	 * @param articleId 
	 * @return object
	 */
	public TArticle getById(Long articleId) {
		return (TArticle)this.baseDao.load(TArticle.class, articleId);
	}
	
	/**
	 * 分页获取文章数据
	 * @param currentPage, pageSize
	 * @return 集合
	 */
	public List<TArticle> getPart(int currentPage, int pageSize) {
		
	
		List<TArticle> tArticleList=new ArrayList<TArticle>();
		String hql="select a.articleId,a.userName,a.articleTitle,a.articleContent,a.articleCreateTime" +
		" from TArticle as a order by a.articleCreateTime desc";
		List list=this.baseDao.find(hql, (currentPage-1)*pageSize, pageSize);
		Iterator it=list.iterator();
		while(it.hasNext()){
			TArticle tArticle=new TArticle();
			Object[] results=(Object[])it.next();
			
			tArticle.setArticleId((Long)results[0]);
			tArticle.setUserName((String)results[1]);
			tArticle.setArticleTitle((String)results[2]);
			tArticle.setArticleContent((String)results[3]);
			tArticle.setArticleCreateTime((Date)results[4]);
			tArticleList.add(tArticle);//装载tArticle对象
			
		}
		
		return tArticleList;
	}
	
	/**
	 * 获取数据库中数据的条数
	 * @return int
	 */
	public int getCount() {
		int count=0;
		String hql="select a.articleId from TArticle as a";
		List list=this.baseDao.find(hql);
		if(list.size()>0){
			count=list.size();
		}
		return count;
	}

	
	

}
